package com.anche.cywebnew.Service.logManager.Impl;

import com.anche.cywebnew.DAO.logManager.LogMapper;
import com.anche.cywebnew.Pojo.logManager.LogInfo;
import com.anche.cywebnew.Service.logManager.LogService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class LogServiceImpl implements LogService {

    private final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final SimpleDateFormat shortDateFormater = new SimpleDateFormat("yyyy-MM-dd");
    private final SimpleDateFormat longDateFormater = new SimpleDateFormat("yyyyMMddHHmmss");

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private LogMapper logMapper;

    @Override
    public void insertInterLog(String interface_Condition, String interface_Result, String requester, String terminal_Id) {

    }

    @Override
    public void insertLogInfo(String operateCondition, String operateType, String operateResult) {

    }

    @Override
    public void insertLogInfo(String operateCondition, String operateType, String operateResult, String userid, String username, String organ) {
        // TODO Auto-generated method stub
        String insertType = "0";
        if (operateType.contains("查询")) {
            insertType = "1";
        } else if (operateType.contains("新增")) {
            insertType = "2";
        } else if (operateType.contains("添加")) {
            insertType = "2";
        } else if (operateType.contains("插入")) {
            insertType = "2";
        } else if (operateType.contains("修改")) {
            insertType = "3";
        } else if (operateType.contains("编辑")) {
            insertType = "3";
        } else if (operateType.contains("删除")) {
            insertType = "4";
        } else if (operateType.contains("登陆")) {
            insertType = "0";
        } else if (operateType.contains("登录")) {
            insertType = "0";
        } else {
            insertType = operateType;
        }
        String operateTime = longDateFormater.format(new Date());
        operateCondition = operateCondition.replaceAll("'", "''");
        String jyw = DigestUtils.md5Hex(userid + insertType + operateTime + operateResult);
        LogInfo logInfo = new LogInfo();
        logInfo.setUser_id(userid);
        logInfo.setUser_name(username);
        logInfo.setOrganization(organ);
        logInfo.setTerminal_id(getRemoteHost(request));
        logInfo.setOperate_type(insertType);
        logInfo.setOperate_time(operateTime);
        logInfo.setOperate_condition(operateCondition);
        logInfo.setOperate_result(operateResult);
        logInfo.setTerminal_type("1");
        logInfo.setJyw(jyw);
        logMapper.insertLogInfo(logInfo);
    }

    @Override
    public void insertLogInfo(String operateCondition, String operateType, String operateResult, String userid, String username, String organ, String terminal_id) {
        // TODO Auto-generated method stub
        String insertType = "0";
        if (operateType.contains("查询")) {
            insertType = "1";
        } else if (operateType.contains("新增")) {
            insertType = "2";
        } else if (operateType.contains("添加")) {
            insertType = "2";
        } else if (operateType.contains("插入")) {
            insertType = "2";
        } else if (operateType.contains("修改")) {
            insertType = "3";
        } else if (operateType.contains("编辑")) {
            insertType = "3";
        } else if (operateType.contains("删除")) {
            insertType = "4";
        } else if (operateType.contains("登陆")) {
            insertType = "0";
        } else if (operateType.contains("登录")) {
            insertType = "0";
        } else {
            insertType = operateType;
        }
        String operateTime = longDateFormater.format(new Date());
        operateCondition = operateCondition.replaceAll("'", "''");
        String jyw = DigestUtils.md5Hex(userid + insertType + operateTime + operateResult);
        LogInfo logInfo = new LogInfo();
        logInfo.setUser_id(userid);
        logInfo.setUser_name(username);
        logInfo.setOrganization(organ);
        logInfo.setTerminal_id(terminal_id);
        logInfo.setOperate_type(insertType);
        logInfo.setOperate_time(operateTime);
        logInfo.setOperate_condition(operateCondition);
        logInfo.setOperate_result(operateResult);
        logInfo.setTerminal_type("1");
        logInfo.setJyw(jyw);
        logMapper.insertLogInfo(logInfo);
    }

    @Override
    public void insertManageLog(String operate_Condition, String operateResult, String operate_type) {

    }

    @Override
    public void insertSecurityLog(String logtype, String oerate_content) {

    }

    public String getRemoteHost(javax.servlet.http.HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }
}
